package ltd.smallwang;

/**
 * @author justd
 * @date 2024-10-02 09:18
 */
public class Leetcode0274 {

  public int hIndex(int[] citations) {
//    return method01(citations);
    return method02(citations, 0, citations.length);
  }

  /**
   * 二分法
   */
  public int method02(int[] citations, int begin, int end) {
    if (begin == end) {
      return begin;
    }
    int mid = (begin + end + 1) / 2;
    if (isValid(citations, mid)) {
      return method02(citations, mid, end);
    }
    return method02(citations, begin, mid - 1);
  }

  /**
   * 暴力解法
   */
  public int method01(int[] citations) {
    int result = 0;
    for (int i = 1; i <= citations.length; i++) {
      if (isValid(citations, i)) {
        result = i;
      }
    }
    return result;
  }

  private boolean isValid(int[] citations, int h) {
    int count = 0;
    for (int citation : citations) {
      if (citation >= h) {
        count++;
      }
    }
    return count >= h;
  }
}
